package com.yss.reportworld.service.impl;

import java.util.*;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yss.common.core.constant.RequestDataHelper;
import com.yss.common.core.domain.TreeNode;
import com.yss.common.core.exception.BusinessException;
import com.yss.common.core.utils.StringUtils;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.reportworld.domain.arrwrap.TcRepNode;
import com.yss.reportworld.enums.ReportWorldEnum;
import com.yss.reportworld.mapper.arrwrap.TcRepNodeMapper;
import com.yss.reportworld.util.TableInfoUtil;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yss.reportworld.service.ITpGlExhibDatadictService;
import com.yss.reportworld.mapper.TpGlExhibDatadictMapper;
import com.yss.reportworld.domain.TpGlExhibDatadict;

/**
 * 报表世界字典Service业务层处理
 *
 * @author yss
 * @date 2023-03-26
 */
@Slf4j
@Service
public class TpGlExhibDatadictServiceImpl extends ServiceImpl<TpGlExhibDatadictMapper, TpGlExhibDatadict> implements ITpGlExhibDatadictService {
    @Autowired
    private TcRepNodeMapper repNodeMapper;
    @Autowired
    private TpGlExhibDatadictMapper tpGlExhibDatadictMapper;

    /**
     * 查询报表世界字典列表
     *
     * @param tpGlExhibDatadict 报表世界字典
     * @return 报表世界字典
     */
    @Override
    public List<TpGlExhibDatadict> selectTpGlExhibDatadictList(TpGlExhibDatadict tpGlExhibDatadict) {
        try {
            TableInfoUtil.handleTableVersion(tpGlExhibDatadict.getHisVersion()); // 替换表名
            QueryWrapper<TpGlExhibDatadict> wrapper = new QueryWrapper();
            wrapper.orderByAsc("KEY_SEQ");
            if (StringUtils.isNotEmpty(tpGlExhibDatadict.getClassId())) {
                wrapper.eq("CLASS_ID", tpGlExhibDatadict.getClassId());
            }
            if (StringUtils.isNotEmpty(tpGlExhibDatadict.getKeyId())) {
                wrapper.like("KEY_ID", tpGlExhibDatadict.getKeyId());
            }
            if (StringUtils.isNotEmpty(tpGlExhibDatadict.getKeyName())) {
                wrapper.like("KEY_NAME", tpGlExhibDatadict.getKeyName());
            }
            return tpGlExhibDatadictMapper.selectList(wrapper);
        } catch (Exception ex) {
            throw new BusinessException("获取字典信息失败" + ex.getMessage());
        } finally {
            RequestDataHelper.removeThreadLocal();
        }
    }

    /**
     * 查询字段，分版本按照 classID分组
     *
     * @param tpGlExhibDatadict
     * @return
     */
    public List<TpGlExhibDatadict> queryTpGlExhibDatadictGroupByClassId(TpGlExhibDatadict tpGlExhibDatadict) {
        try {
            TableInfoUtil.handleTableVersion(tpGlExhibDatadict.getHisVersion()); // 替换表名
            QueryWrapper<TpGlExhibDatadict> datadictQueryWrapper = new QueryWrapper<>();
            datadictQueryWrapper.select("CLASS_ID,CLASS_NAME");
            datadictQueryWrapper.groupBy("CLASS_ID");
            datadictQueryWrapper.orderByAsc("CLASS_ID");
            return tpGlExhibDatadictMapper.selectList(datadictQueryWrapper);
        } catch (Exception ex) {
            throw new BusinessException("获取字典信息失败" + ex.getMessage());
        } finally {
            RequestDataHelper.removeThreadLocal();
        }
    }

    /**
     * 获取报表世界监管机构和监管规则数结构信息
     * 导出部署包
     *
     * @return
     */
    public List<TreeNode> queryRegulationOrg() {
        QueryWrapper<TpGlExhibDatadict> wrapper = new QueryWrapper();
        wrapper.eq("CLASS_ID", ReportWorldEnum.CLASS_ID_A00010.getCode());
        wrapper.orderByAsc("KEY_SEQ");
        List<TpGlExhibDatadict> list = this.list(wrapper);
        List<TreeNode> nodes = new ArrayList<>();
        if (StringUtils.isEmpty(list)) {
            return nodes;
        }
        // 只到监管报表，只查询一次，不递归
        for (TpGlExhibDatadict datadict : list) {
            if (StringUtils.isNull(datadict) || StringUtils.isEmpty(datadict.getKeyId())) {
                continue;
            }
            TreeNode treeNode = new TreeNode();
            treeNode.setId(datadict.getId());
            treeNode.setLabel(datadict.getKeyName());
            treeNode.setParentId(datadict.getId());
            TcRepNode tcRepNode = new TcRepNode();
            tcRepNode.setParentId(datadict.getKeyId());
            if (StringUtils.isEmpty(tcRepNode.getParentId())) {
                continue;
            }
            QueryWrapper<TcRepNode> wrapperNode = new QueryWrapper();
            wrapperNode.orderByAsc("NODE_NAME");
            wrapperNode.eq("PARENT_ID", tcRepNode.getParentId());
            List<TcRepNode> tcRepNodeList = repNodeMapper.selectList(wrapperNode);
            if (StringUtils.isNull(tcRepNodeList)) {
                continue;
            }
            List<TreeNode> treeNodeList = new ArrayList<>();
            for (TcRepNode node : tcRepNodeList) {
                TreeNode treeNodeChildren = new TreeNode();
                treeNodeChildren.setId(node.getNodeId());
                treeNodeChildren.setLabel(node.getNodeName());
                treeNodeChildren.setParentId(datadict.getId());
                treeNodeList.add(treeNodeChildren);
            }
            treeNode.setChildren(treeNodeList);
            nodes.add(treeNode);
        }
        return nodes;

    }


}
